﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES4003
{
    [Plug(ID = "cMES4003",
          Name = "WIPKPS资料",
          Description = "WIPKPS资料维护",
          Parent = "4,数据查询")]
    public partial class cMES4003 : StdView
    {
        public cMES4003()
        {
            InitializeComponent();
            datalist.CellMouseDoubleClick += Datalist_CellMouseDoubleClick;
        }
        private void cMES1000_Load(object sender, EventArgs e)
        {
            //this.LoadVendorList();
        }

        private void LoadVendorList()
        {
            ExecutionResult<dynamic> res = Services.ServiceMethod.GetVendorList();

            if (!res.Status)
            {
                ShowMessage(res.Message, MsgType.NG);

                return;
            }

            datalist.DataSource = res.Data;

            datalist.ClearSelection();
        }

        private void Datalist_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.RowIndex > -1)
            //{
            //    Edit edit = new Edit("EDIT", new object[]
            //    {
            //        datalist.SelectedRows[0].Cells[0].Value,
            //        datalist.SelectedRows[0].Cells[1].Value,
            //        datalist.SelectedRows[0].Cells[2].Value,
            //        datalist.SelectedRows[0].Cells[3].Value,
            //        datalist.SelectedRows[0].Cells[4].Value,
            //        datalist.SelectedRows[0].Cells[5].Value
            //    });

            //    DialogResult result = edit.ShowDialog();

            //    if (result == DialogResult.OK)
            //    {
            //        this.ShowMessage("数据修改成功！", MsgType.OK);

            //        this.LoadVendorList();
            //    }

            //    if (result == DialogResult.No)
            //    {
            //        this.ShowMessage("数据修改失败！", MsgType.NG);
            //    }
            //}
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sn = string.Empty, kpn = string.Empty, kps = string.Empty;
            if (string.IsNullOrEmpty(txtSearch.Text))
            {
                sn = txtSearch.Text;
            }
            if (string.IsNullOrEmpty(txtPartNo.Text))
            {
                kpn = txtPartNo.Text;
            }
            if (string.IsNullOrEmpty(txtKeyPartSn.Text))
            {
                kps = txtKeyPartSn.Text;
            }

            if (!string.IsNullOrEmpty(kpn))
            {
                if (string.IsNullOrEmpty(sn))
                {
                    ShowMessage("流水号必须输入，才可以同时查询料号信息", MsgType.NG);
                    return;
                }
            }

            string vm = JsonConvert.SerializeObject(new
            {
                SN = sn,
                KPN = kpn,
                KP_SN = kps
            });

            ExecutionResult<dynamic> res = Services.ServiceMethod.GetWipKpsInfo(vm);

            if (!res.Status)
            {
                ShowMessage(res.Message, MsgType.NG);
            }
            else
            {
                if (res.Data.Count > 0)
                {
                    datalist.DataSource = res.Data;
                    ShowMessage("查询成功", MsgType.OK);
                }
                else
                {
                    datalist.Rows.Clear();
                    ShowMessage($"没有查到：{txtSearch.Text}", MsgType.NG);
                }

                datalist.ClearSelection();
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            //Edit edit = default(Edit);

            //if (((ToolStripButton)sender).Tag.Equals("ADD"))
            //{
            //    edit = new Edit("ADD");

            //    DialogResult result = edit.ShowDialog();

            //    if (result == DialogResult.OK)
            //    {
            //        this.ShowMessage("数据添加成功！", MsgType.OK);

            //        this.LoadVendorList();
            //    }

            //    if (result == DialogResult.No)
            //    {
            //        this.ShowMessage("数据添加失败！", MsgType.NG);
            //    }
            //}

            //if (((ToolStripButton)sender).Tag.Equals("EDIT"))
            //{
            //    if (datalist.SelectedRows.Count < 1)
            //    {
            //        ShowMessage("请选择一个供应商信息进行编辑.", MsgType.NG);
            //        return;
            //    }
            //    edit = new Edit("EDIT", new object[] 
            //    {
            //        datalist.SelectedRows[0].Cells[0].Value,
            //        datalist.SelectedRows[0].Cells[1].Value,
            //        datalist.SelectedRows[0].Cells[2].Value,
            //        datalist.SelectedRows[0].Cells[3].Value,
            //        datalist.SelectedRows[0].Cells[4].Value,
            //        datalist.SelectedRows[0].Cells[5].Value
            //    });

            //    DialogResult result = edit.ShowDialog();

            //    if (result == DialogResult.OK)
            //    {
            //        this.ShowMessage("数据修改成功！", MsgType.OK);

            //        this.LoadVendorList();
            //    }

            //    if (result == DialogResult.No)
            //    {
            //        this.ShowMessage("数据修改失败！", MsgType.NG);
            //    }
            //}

            //if (((ToolStripButton)sender).Tag.Equals("DELETE"))
            //{
            //    if (datalist.SelectedRows.Count < 1)
            //    {
            //        ShowMessage("请选择一个需要删除的供应商信息.", MsgType.NG);
            //        return;
            //    }

            //    DialogResult dialog = MessageBox.Show($"确定要删除{datalist.SelectedRows[0].Cells[0].Value}吗？", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

            //    if (dialog == DialogResult.Yes)
            //    {
            //        ExecutionResult result = Services.ServiceMethod.DeleteVendor(datalist.SelectedRows[0].Cells[0].Value.ToString());

            //        if (result.Status && result.ExecuteCount > 0)
            //        {
            //            this.ShowMessage($"数据{datalist.SelectedRows[0].Cells[0].Value}删除成功！", MsgType.OK);

            //            this.LoadVendorList();
            //        }
            //    }
            //}
        }
    }
}
